package org.example.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectResult;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * web读写案例
 *
 * @author Jiaju Zhuang
 **/
@Controller
public class OfficeTest {

    @Autowired
    private TestService testService;

    public static void main3(String[] args) {

        // 您的工程中可以只保留一个全局的ObsClient实例
// ObsClient是线程安全的，可在并发场景下使用
        ObsClient obsClient = null;
        try {
            String endPoint = "http://obs.cn-east-3.myhuaweicloud.com";
            String ak = "EWGYXHBAMGH2GMUTGFFZ";
            String sk = "477LboFMzhzHPvPMN4p4B5fDopUhdCON0WsC49QB";
            // 创建ObsClient实例
            obsClient = new ObsClient(ak, sk, endPoint);
            // 调用接口进行操作，例如上传对象
            PutObjectResult response = obsClient.putObject("dev-test-obs", "测试.xlsx", new File("D:\\google download\\测试.xlsx"));  // localfile为待上传的本地文件路径，需要指定到具体的文件名
            System.out.println(response);
        } catch (ObsException e) {
            System.out.println("HTTP Code: " + e.getResponseCode());
            System.out.println("Error Code:" + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());

            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
        } finally {
            // 关闭ObsClient实例，如果是全局ObsClient实例，可以不在每个方法调用完成后关闭
            // ObsClient在调用ObsClient.close方法关闭后不能再次使用
            if (obsClient != null) {
                // obsClient.close();
            }
        }

//        try {
//            URL url = new URL("https://dev-test-obs.obs.cn-east-3.myhuaweicloud.com/web/%E6%B5%8B%E8%AF%95.xlsx");
//            URLConnection connection = url.openConnection();
//            InputStream is = connection.getInputStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(is, "gb2312"));
//            System.out.println(br);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

    /**
     * 系统文件在线预览接口
     */
    @GetMapping("/api/file/onlinePreview")
    public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception{
        testService.onlinePreview(url,response);
    }





}
